# 目标
# 1. 拷贝视频文件并修改后缀
# 2. 修改图片的分辨率
# 3. 批量完成

import os
import cv2
from pathlib import Path


source_path = r"D:\桌面\2022研电赛\line_dataset"
sink_path = r"D:\桌面\2022研电赛\Line Dataset"

if not os.path.exists(source_path) or not os.path.exists(sink_path):
    print('Path not exit!')
    exit()

videos_list = os.listdir(source_path)

for video in videos_list:
    video_path = os.path.join(source_path, video)
    if Path(video_path).suffix in ['.MOV', '.mov']:
        # 修改后缀名
        dis_video_name = video
        dis_video_name = dis_video_name.replace(str(dis_video_name).split('.')[-1], 'avi')
        dis_path = os.path.join(sink_path, dis_video_name)

        # 进行转换
        cap = cv2.VideoCapture(video_path)
        success, _ = cap.read()
        # 重新合成的视频在原文件夹，如果需要分开，可以修改file_n
        video_writer = cv2.VideoWriter(dis_path, cv2.VideoWriter_fourcc(*'XVID'), 25, (620, 360))
        while success:
            success, vid1 = cap.read()
            try:
                vid = cv2.resize(vid1, (620, 360), interpolation=cv2.INTER_LINEAR)  # 希望的分辨率大小可以在这里改
                video_writer.write(vid)
            except:
                break



